// pages/messageDetail/messageDetail.js
import dayjs from 'dayjs';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: null,
    projectContent: null
  },

  // 时间处理
  timeformatt(time) {
    return dayjs(time).format("YYYY/MM/DD");
  },
  /**
   * 
   * */
  async goHandle(e) {
    let {
      key
    } = getApp().utils.getParameter(e);
    if (key === undefined) return;
    let keyLabel = "";
    if (key == 1) {
      keyLabel = "同意";
    }
    if (key == 2) {
      keyLabel = "拒绝";
    }
    let formData = {
      "linkId": this.data.content.linkId,
      "linkStatusValue": Number(key),
      "linkStatusLabel": keyLabel
    }
    let res = await getApp().http.requestLinkHandle(formData);
    if (res.errorCode === 200) {
      wx.showModal({
        content: '操作成功,已通知买家。',
        showCancel: false,
        success() {
          wx.navigateBack();
        }
      })
    } else {
      wx.showModal({
        content: res.errorMessage,
        showCancel: false,
      })
    }
  },
  /**
   * 获取线索详情
   * */
  async getProjectDetails(id) {
    let res = await getApp().http.getProjectDetail({
      projectId: id
    });
    if (res.errorCode === 200) {
      this.setData({
        projectContent: res.data,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('页面参数', options);
    this.getData({
      linkId: options.id
    });
  },
  async getData(formData) {
    let res = await getApp().http.requestLinkDetail(formData);
    if (res.errorCode === 200) {
      console.log('请求成功', res.data);
      this.getProjectDetails(res.data.linkProjectId);
      const content = res.data;
      if (content.updateTime) {
        content.updateTime = this.timeformatt(content.updateTime);
      }
      this.setData({
        content: content
      })
    } else {
      wx.showModal({
        content: '请求失败',
        showCancel: false,
        confirmText: "返回",
        complete: (res) => {
          if (res.confirm) {
            wx.navigateBack();
          }
        }
      })
    }
  },
  // 拨打电话
  callPhone() {
    let phone = this.data.content.linkFromMobile;
    if (phone) {
      wx.makePhoneCall({
        phoneNumber: (phone),
        success() {
          wx.showToast({
            icon: "none",
            title: '成功',
          })
        },
        fail() {
          wx.showToast({
            icon: "none",
            title: '失败',
          })
        }
      })
    } else {
      wx.showToast({
        title: '暂无联系号码',
        icon: "error"
      })
    }
  },
  callSellerPhone() {
    var that = this;
    wx.showModal({
      content: '是否立即拨打电话联系',
      complete: (resf) => {
        if (resf.confirm) {
          that.callPhone();
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})